#!/bin/sh


if [ -n "$_VL_IMPORT_GENERATE_FOR_BUILD" ] ; then
	return;
fi
_VL_IMPORT_GENERATE_FOR_BUILD=0

######################################################
######################################################
# 文件生成方法
######################################################
######################################################

# 生成构建配置文件
# 参数 1 : 文件路径
# 
# 返回值 : 生成成功返回 0
function generateBuildConfig() {
	cat > "$1" << EOF 
# Project Name
VL_NAME=


# Project Type:
# exec - executable project
# lib  - library project, support dynamic library and static library
VL_TYPE=lib


# Use dependencies
# dependency format: "[ProjectName] [git url] [version] [link type]"
# link type:
# static  - static link
# dynamic - dynamic link
# example:
# VL_DEP = (vl_semphore https://gitee.com/CimZzz/vl_semphore.git master static)
#
#
# if need multi-line dependencies, use '\', like this:
#
# VL_DEP = (vl_semphore https://gitee.com/CimZzz/vl_semphore.git master static \\
# vl_semphore https://gitee.com/CimZzz/vl_semphore.git master static \\
# vl_semphore https://gitee.com/CimZzz/vl_semphore.git master static \\
# vl_semphore https://gitee.com/CimZzz/vl_semphore.git master static \\
# any more ... )
VL_DEP=
EOF
	if [ $? -ne 0 ] ; then
		return 1
	fi

	return 0
}

# 生成编译工具链配置
# 参数 1 : 文件地址
# 
# 返回值 : 创建成功返回 0
function generateToolChains() {
	cat > "$1" << EOF
# Search for dependent libraries within DIR (or the compiler's sysroot if not specified).
VL_SYSROOT=

# cross-compile to build programs to run on HOST
VL_HOST=

# configure for building on BUILD
VL_BUILD=

# install architecture-independent files in PREFIX
# default [/usr/local]
VL_PREFIX=

# C compiler command
VL_CC=

# C++ compiler command
VL_CXX=

# ar compiler command
VL_AR=

# ranlib compiler command
VL_RANLIB=
EOF

	if [ $? -ne 0 ] ; then
		return 1
	fi

	return 0
}



# 生成编译配置文件
# 参数 1 : 生成文件路径
#
# 返回值: 生成成功返回 0
function generateCompileOptions() {
	if checkFile "vl_compile_options.sh" ; then
		return 0
	fi
	cat > "vl_compile_options.sh" << EOF
#!/bin/sh

# Configure arguments

# C++ compiler flags, e.g. -std=c++11
VL_CONFIG_ARGS_CXXFLAGS=

# libraries to pass to the linker, e.g. -l<library>
VL_CONFIG_ARGS_LIBS=

# linker flags, e.g. -L<lib dir> if you have libraries in a nonstandard directory <lib dir>
VL_CONFIG_ARGS_LDFLAGS=

# C/C++ preprocessor flags, e.g. -I<include dir> if you have headers in a nonstandard directory <include dir>
VL_CONFIG_ARGS_CPPFLAGS=

# C compiler flags
VL_CONFIG_ARGS_CFLAGS=

# User-defined run-time library search path.
VL_CONFIG_ARGS_LT_SYS_LIBRARY_PATH=

# C++ preprocessor
VL_CONFIG_ARGS_CXXCPP=
EOF
	if [ $? -ne 0 ] ; then
		return 1
	fi

	return 0
}

# 创建项目级 Makefile.am
#
# 返回值: 创建成功返回 0
function generateProjectMakefile() {
	if checkFile "Makefile.am" ; then
		return 0
	fi

	cat > "Makefile.am" << EOF
SUBDIRS = src
EOF
	if [ $? -ne 0 ] ; then
		return 1
	fi

	return 0
}

# 创建项目 .gitignore
#
# 返回值 : 创建成功返回 0
function generateGitIgnore() {
	if checkFile ".gitignore" ; then
		return 0
	fi

	cat > ".gitignore" << EOF
# http://www.gnu.org/software/automake

Makefile.in
/ar-lib
/mdate-sh
/py-compile
/test-driver
/ylwrap
.deps/
.dirstamp

# http://www.gnu.org/software/autoconf

autom4te.cache
/autoscan.log
/autoscan-*.log
/aclocal.m4
/compile
/config.cache
/config.guess
/config.h.in
/config.log
/config.status
/config.sub
/configure
/configure.scan
/depcomp
/install-sh
/missing
/stamp-h1

# https://www.gnu.org/software/libtool/

/ltmain.sh

# http://www.gnu.org/software/texinfo

/texinfo.tex

# http://www.gnu.org/software/m4/

m4/libtool.m4
m4/ltoptions.m4
m4/ltsugar.m4
m4/ltversion.m4
m4/lt~obsolete.m4

# Generated Makefile
# (meta build system like autotools,
# can automatically generate from config.status script
# (which is called by configure script))
Makefile
.vscode
.vl-build
vl_test_build
EOF
	if [ $? -ne 0 ] ; then
		return 1
	fi

	return 0
}

# 创建可执行文件 configure.ac
#
# 返回值 : 创建成功返回
function generateExecConfigureAc() {
	if checkFile "configure.ac" ; then
		return 0
	fi

	cat > "configure.ac" << EOF
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.71])
AC_INIT([FULL-PACKAGE-NAME],[VERSION],[BUG-REPORT-ADDRESS])
AC_CONFIG_SRCDIR([src/$VL_NAME.cpp])
AC_CONFIG_HEADERS([config.h])
AM_INIT_AUTOMAKE([subdir-objects])

# Checks for programs.
AC_PROG_CXX
AC_PROG_INSTALL
AC_PROG_MAKE_SET

# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.
AC_CONFIG_FILES([Makefile
                 src/Makefile])
AC_OUTPUT
EOF
	if [ $? -ne 0 ] ; then
		return 1
	fi

	return 0
}

# 创建库文件 configure.ac
#
# 返回值 : 创建成功返回
function generateLibraryConfigureAc() {
	if checkFile "configure.ac" ; then
		return 0
	fi

	cat > "configure.ac" << EOF
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.71])
AC_INIT([FULL-PACKAGE-NAME],[VERSION],[BUG-REPORT-ADDRESS])
AC_CONFIG_SRCDIR([src/$VL_NAME.cpp])
AC_CONFIG_HEADERS([config.h])
AM_INIT_AUTOMAKE([subdir-objects])


AC_ARG_ENABLE(
[test_program],
AS_HELP_STRING(
	[--enable-test-program],
	[enable test program build, default disable],
),
[
	case "\${enableval}" in
	yes)
    	test_program=true
	;;
	no)
    	test_program=false
	;;
	*)
	;;
	esac
],
[test_program=false]
)
AM_CONDITIONAL([TESTPROGRAM], [test "x\$test_program" = "xtrue"])

# Checks for programs.
AC_PROG_CXX
AC_PROG_INSTALL
AC_PROG_MAKE_SET
LT_INIT

# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.
AC_CONFIG_FILES([Makefile
                 src/Makefile])
AC_OUTPUT
EOF
	if [ $? -ne 0 ] ; then
		return 1
	fi

	return 0
}

# 创建可执行文件 Makefile.am
#
# 返回值 : 创建成功返回 0
function generateExecMakefile() {
	if checkFile "Makefile.am" ; then
		return 0
	fi

	cat > "Makefile.am" << EOF
bin_PROGRAMS = ${VL_NAME}
${VL_NAME}_SOURCES = ${VL_NAME}.cpp
EOF
	if [ $? -ne 0 ] ; then
		return 1
	fi

	return 0
}

# 创建库文件 Makefile.am
#
# 返回值 : 创建成功返回 0
function generateLibraryMakefile() {
	if checkFile "Makefile.am" ; then
		return 0
	fi

	cat > "Makefile.am" << EOF
lib_LTLIBRARIES = lib${VL_NAME}.la
lib${VL_NAME}_la_SOURCES = ${VL_NAME}.h \\
	${VL_NAME}.cpp

nobase_include_HEADERS = ${VL_NAME}.h


if TESTPROGRAM
bin_PROGRAMS = test_program
test_program_SOURCES = test/test.cpp
test_program_LDADD = lib${VL_NAME}.la
endif
EOF
	if [ $? -ne 0 ] ; then
		return 1
	fi

	return 0
}

# 创建可执行文件源文件
# 
# 返回值 : 创建成功返回 0
function generateExecSource() {
	if checkFile "${VL_NAME}.cpp" ; then
		return 0
	fi
	
	cat > "${VL_NAME}.cpp" << EOF
#include <cstdio>

int main(int argc, char** argv) {
	printf("hello world\n");
	return 0;
}
EOF

	if [ $? -ne 0 ] ; then
		return 1
	fi

	return 0
}

# 创建库 test.cpp 源文件
# 
# 返回值 : 创建成功返回 0
function generateTestProgram() {
	if checkFile "test.cpp" ; then
		return 0
	fi

	cat > "test.cpp" << EOF
#include "${VL_NAME}.h"
#include <cstdio>

int main(int argc, char** argv) {
	printf("hello world\n");
	return 0;
}
EOF

	if [ $? -ne 0 ] ; then
		return 1
	fi

	return 0
}